package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.icss.beans.EnrollQAVO;
import com.icss.daos.EnrollQADao;

/**
 * @author 韦锦良
 *　类的作用：招生问答serlvet控制类
 * 最后修改时间：2010-3-13　下午03:56:35
 * 最后的改动内容：初始版本
 */
@SuppressWarnings("serial")
public class EnrollQAServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");

		EnrollQADao dao = new EnrollQADao();

		//添加招生问答
		if ("add".equals(action)) {

			String question = request.getParameter("question").trim();
			String answer = request.getParameter("answer").trim();

			String sql = "insert into EnrollQA(question, answer) values('" + question + "','" + answer + "')";

			boolean result = dao.execute(sql);

			PrintWriter out = response.getWriter();

			response.setContentType("text/html");

			if (result) {
				out.write("<script>alert('添加成功');window.location.href='enroll?action=admin_list';</script>");
			} else {
				out.write("<script>alert('添加失败');window.location.href='enroll?action=admin_list';</script>");
			}

			out.close();

		}

		//删除招生问答
		if ("delete".equals(action)) {

			String qaID = request.getParameter("qaID");

			String sql = "delete from EnrollQA where  qaID=" + qaID;

			boolean result = dao.execute(sql);

			PrintWriter out = response.getWriter();

			response.setContentType("text/html");

			if (result) {
				out.write("<script>alert('删除成功');window.location.href='enroll?action=admin_list';</script>");
			} else {
				out.write("<script>alert('删除失败');window.location.href='enroll?action=admin_list';</script>");
			}
			out.close();

		}

		//修改招生问答
		if ("update".equals(action)) {

			String qaID = request.getParameter("qaID");
			String question = request.getParameter("question").trim();
			String answer = request.getParameter("answer").trim();

			String sql = "update EnrollQA set question='" + question + "', answer='" + answer + "' where qaID=" + qaID;

			boolean result = dao.execute(sql);

			PrintWriter out = response.getWriter();

			response.setContentType("text/html");

			if (result) {
				out.print("<script>alert('修改成功');window.location.href='enroll?action=admin_list';</script>");
			} else {
				out.print("<script>alert('修改失败');window.location.href='enroll?action=admin_list';</script>");
			}
			out.close();
		}

		//管理员身份请求招生问答
		if ("admin_list".equals(action)) {

			ArrayList<EnrollQAVO> vos = dao.getAllEnrollQA();
			request.setAttribute("admin_list", vos);
			request.getRequestDispatcher("/page/zhaosheng/admin_list.jsp").forward(request, response);
		}

		//显示招生问答
		if ("showWenda".equals(action)) {

			String qaID = request.getParameter("qaID");

			EnrollQAVO vo = dao.getEnrollQAVOById(qaID);

			PrintWriter out = response.getWriter();
			JSONObject obj = JSONObject.fromObject(vo);
			out.print(obj);

		}
		
		//请求修改招生问答
		if ("updateRequest".equals(action)) {

			String qaID = request.getParameter("qaID");

			EnrollQAVO vo = dao.getEnrollQAVOById(qaID);

			request.setAttribute("enrollQA", vo);

			JSONObject enrollQA = JSONObject.fromObject(vo);

			PrintWriter out = response.getWriter();

			out.print(enrollQA);

			out.close();

		}

		//为前台显示招生问答
		if ("forward_list".equals(action)) {

			ArrayList<EnrollQAVO> vos = dao.getAllEnrollQA();

			request.setAttribute("forward_list", vos);

			request.getRequestDispatcher("/page/zhaosheng/forward_wenda.jsp").forward(request, response);
		}

		dao.close();
	}

}
